import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },

  {
    path: "/equipmentManagementWa",
    name: "equipmentManagementWa",
    component: Layout,
    redirect: "/equipmentManagementWa",
    meta: {
      title: "WA",
      icon: "homepage",
    },
    children: [
      {
        path: "equipmentWa",
        component: () => import("@/views/equipmentManagement/device/index.vue"),
        name: "EquipmentWa",
        meta: {
          title: "设备管理",
          icon: "homepage",
        },
      },
      {
        path: "accountAdminWa",
        name: "AccountAdminWa",
        meta: {
          title: "账号管理",
          icon: "homepage",
        },
        children: [
          {
            path: "accountWa",
            component: () => import("@/views/equipmentManagement/account/index.vue"),
            name: "AccountWa",
            meta: {
              title: "账号列表",
              icon: "homepage",
            },
          },
          {
            path: "derivedRecordWa",
            component: () => import("@/views/equipmentManagement/derivedRecord/index.vue"),
            name: "DerivedRecordWa",
            meta: {
              title: "导出记录",
              icon: "homepage",
            },
          },
        ],
      },
      {
        path: "massTextingWa",
        component: () => import("@/views/equipmentManagement/massTexting/index.vue"),
        name: "MssTextingWa",
        meta: {
          title: "群发",
          icon: "homepage",
        },
      },
      {
        path: "numberManage",
        name: "NumberManage",
        meta: {
          title: "API",
        },
        component: () => import("@/views/equipmentManagement/numberManage/index.vue"),
      },
      {
        path: "chat",
        component: () => import("@/views/equipmentManagement/chat/index.vue"),
        name: "Chat",
        meta: {
          title: "精聊",
          icon: "homepage",
        },
      },
    ],
  },

  {
    path: "/equipmentManagementTg",
    name: "equipmentManagementTg",
    component: Layout,
    redirect: "/equipmentManagementTg",
    meta: {
      title: "TG",
      icon: "homepage",
    },
    children: [
      {
        path: "equipmentTg",
        component: () => import("@/views/equipmentManagementTg/device/index.vue"),
        name: "EquipmentTg",
        meta: {
          title: "设备管理",
          icon: "homepage",
        },
      },
      {
        path: "accountAdminTg",
        name: "AccountAdminTg",
        meta: {
          title: "账号管理",
          icon: "homepage",
        },
        children: [
          {
            path: "accountTg",
            component: () => import("@/views/equipmentManagementTg/account/index.vue"),
            name: "AccountTg",
            meta: {
              title: "账号列表",
              icon: "homepage",
            },
          },
          {
            path: "derivedRecordTg",
            component: () => import("@/views/equipmentManagementTg/derivedRecord/index.vue"),
            name: "DerivedRecordTg",
            meta: {
              title: "导出记录",
              icon: "homepage",
            },
          },
        ],
      },
      {
        path: "apiTg",
        name: "ApiTg",
        meta: {
          title: "API",
        },
        children: [
          {
            path: "numberManageTG",
            name: "NumberManageTG",
            meta: {
              title: "号码管理",
            },
            component: () => import("@/views/equipmentManagementTg/numberManage/index.vue"),
          },
          {
            path: "emailManageTG",
            name: "EmailManageTG",
            meta: {
              title: "邮箱管理",
            },
            component: () => import("@/views/equipmentManagementTg/emailManage/index.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/equipmentManagementViber",
    name: "equipmentManagementViber",
    component: Layout,
    redirect: "/equipmentManagementViber",
    meta: {
      title: "VIBER",
      icon: "homepage",
    },
    children: [
      {
        path: "equipmentViber",
        component: () => import("@/views/equipmentManagementViber/device/index.vue"),
        name: "EquipmentViber",
        meta: {
          title: "设备管理",
          icon: "homepage",
        },
      },
      {
        path: "accountAdminViber",
        name: "AccountAdminViber",
        meta: {
          title: "账号管理",
          icon: "homepage",
        },
        children: [
          {
            path: "accountViber",
            component: () => import("@/views/equipmentManagementViber/account/index.vue"),
            name: "AccountViber",
            meta: {
              title: "账号列表",
              icon: "homepage",
            },
          },
          {
            path: "derivedRecordViber",
            component: () => import("@/views/equipmentManagementViber/derivedRecord/index.vue"),
            name: "DerivedRecordViber",
            meta: {
              title: "导出记录",
              icon: "homepage",
            },
          },
        ],
      },
      {
        path: "NumberManageViber",
        name: "NumberManageViber",
        meta: {
          title: "API",
        },
        component: () => import("@/views/equipmentManagementViber/numberManage/index.vue"),
      },
    ],
  },
  {
    path: "/codeReceiving",
    name: "CodeReceiving",
    component: Layout,
    redirect: "/codeReceiving",
    meta: {
      title: "接码平台",
    },
    children: [
      {
        path: "codeReceivingList",
        component: () => import("@/views/codeReceiving/index.vue"),
        name: "CodeReceivingList",
        meta: {
          title: "接码平台",
          icon: "homepage",
        },
      },
    ],
  },
  // {
  //   path: "/facility",
  //   name: "设备模块",
  //   component: Layout,
  //   redirect: "/facility",
  //   children: [
  //     {
  //       path: "script",
  //       component: () => import("@/views/facility/script/index.vue"),
  //       name: "Script",
  //       meta: {
  //         title: "脚本管理",
  //         icon: "homepage",
  //       },
  //     },
  //   ],
  // },

  {
    path: "/scriptManage",
    name: "ScriptManage",
    component: Layout,
    redirect: "/scriptManage",
    children: [
      {
        path: "scriptManageList",
        component: () => import("@/views/scriptManage/index.vue"),
        name: "ScriptManageList",
        meta: {
          title: "脚本管理 V1",
          icon: "homepage",
        },
      },
      {
        path: "scriptConfigList",
        component: () => import("@/views/scriptManage/configList.vue"),
        name: "ScriptConfigList",
        meta: {
          title: "脚本配置",
          icon: "homepage",
          hidden: true,
        },
      },
    ],
  },
  {
    path: "/platformConfig",
    name: "PlatformConfig",
    component: Layout,
    redirect: "/platformConfig",
    meta: {
      title: "接码平台管理",
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/platformConfig/index.vue"),
        name: "List",
        meta: {
          title: "接码平台管理",
          icon: "homepage",
          roles: ["admin"],
        },
      },
      {
        path: "design",
        component: () => import("@/views/platformConfig/design.vue"),
        name: "Design",
        meta: {
          title: "接码平台配置",
          icon: "homepage",
          hidden: true,
          roles: ["admin"],
        },
      },
    ],
  },

  {
    path: "/oss",
    name: "Oss",
    component: Layout,
    redirect: "/oss",
    children: [
      {
        path: "index",
        component: () => import("@/views/oss/index.vue"),
        name: "OssIndex",
        meta: {
          title: "素材库",
          icon: "homepage",
        },
      },
    ],
  },
  {
    path: "/user",
    name: "User",
    component: Layout,
    redirect: "/user",
    children: [
      {
        path: "admin",
        component: () => import("@/views/user/admin/index.vue"),
        name: "Admin",
        meta: {
          title: "用户管理",
          icon: "homepage",
          roles: ["admin"],
        },
      },
    ],
  },
  {
    path: "/system",
    name: "System",
    component: Layout,
    redirect: "/system",
    children: [
      {
        path: "dict",
        component: () => import("@/views/system/dict/index.vue"),
        name: "Dict",
        meta: {
          title: "字典管理",
          icon: "homepage",
        },
      },
    ],
  },

  {
    path: "/virtualNumber",
    name: "VirtualNumber",
    component: Layout,
    redirect: "/virtualNumber",
    meta: {
      title: "虚拟号码",
      icon: "homepage",
    },
    children: [
      {
        path: "virtualNumberList",
        component: () => import("@/views/virtualNumber/index.vue"),
        name: "VirtualNumberList",
        meta: {
          title: "平台管理",
          icon: "homepage",
        },
      },
      {
        path: "userConfigList",
        component: () => import("@/views/virtualNumber/config/index.vue"),
        name: "UserConfigList",
        meta: {
          title: "配置管理",
          icon: "homepage",
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
